package com.zyx.jwt.util;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

/**
 * @author Yaxi.Zhang
 * @since 2022/9/11 13:54
 */
public class RsaKeyHelper {
    /**
     * 生成私钥
     * @param privateKey 可以存放在redis或者配置文件中
     */
    public static PrivateKey getPrivateKey(byte[] privateKey) throws Exception {
        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(privateKey);
        KeyFactory kf = KeyFactory.getInstance("RSA");
        return kf.generatePrivate(spec);
    }

    /**
     * 生成公钥
     * @param publicKey 可以存放在redis或者配置文件中
     */
    public static PublicKey getPublicKey(byte[] publicKey) throws Exception  {
        X509EncodedKeySpec spec = new X509EncodedKeySpec(publicKey);
        KeyFactory kf = KeyFactory.getInstance("RSA");
        return kf.generatePublic(spec);
    }

    public static void hehe() {

    }


}
